﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;


namespace KFGame
{
    public class KFFile
    {

        /// <summary>
        /// 拿到配置文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static INIParser InitParser(String path)
        {
            string spath = InitFile(path);
            INIParser par = new INIParser();
            par.Open(spath);
            return par;
        }


        /// <summary>
        /// 处理文件路径
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static bool InitFileDirectory(String filepath)
        {
            var path = filepath;
            path = filepath.Substring(0, filepath.LastIndexOf('/'));
            if (Directory.Exists(path))
                return false;

            Directory.CreateDirectory(path);
            return true;
        }

        /// <summary>
        /// 配置文件路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="suffix"></param>
        /// <returns></returns>
        public static String InitFile(String path, String suffix = ".ini")
        {

            string iniPath = Application.streamingAssetsPath + "/" + path + suffix;

            if (File.Exists(iniPath))
                return iniPath;

            InitFileDirectory(iniPath);

            FileStream fs = new FileStream(iniPath, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            TextAsset soureT = Resources.Load(path) as TextAsset;
            if (soureT != null)
                sw.Write(soureT.text);
            sw.Close();
            fs.Close();

            return iniPath;
        }
        public static string temp = ".temp";
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path"></param>
        public static void CleanFile(string path)
        {
            if (canread(path))
            {
                File.Delete(path + temp);
                File.Delete(path);
            }
        }

        /// <summary>
        /// 写入所有行
        /// 复盖模式
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="datas">数据</param>
        public static void WriteAllLinks(string path, string[] datas)
        {
            if (canread(path))
            {
                File.WriteAllLines(path + temp, datas);
                byte[] bt = File.ReadAllBytes(path + temp);
                File.WriteAllBytes(path, bt);
            }
        }

        /// <summary>
        /// 读取所有行
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string[] ReadAllLinks(string path)
        {
            if (canread(path)) return File.ReadAllLines(path);
            return null;
        }

        /// <summary>
        ///    双存区文件处理
        ///    保证在写入文件时，断电保护
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool canread(string path)
        {
            bool sf = File.Exists(path);
            bool tf = File.Exists(path + temp);
            if (!sf) File.Create(path).Dispose();
            if (!tf) File.Create(path + temp).Dispose();

            FileInfo si = new FileInfo(path);
            FileInfo ti = new FileInfo(path + temp);
            if (ti.Length == si.Length) return true;

            if (ti.Length > si.Length)
            {
                byte[] bt = File.ReadAllBytes(path + temp);
                File.WriteAllBytes(path, bt);
                return true;
            }

            if (ti.Length < si.Length)
            {
                byte[] bt = File.ReadAllBytes(path);
                File.WriteAllBytes(path + temp, bt);
                return true;
            }
            return false;
        }
    }
}
